package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.*;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.config.Config;
import br.com.telescope.t2.exception.ApplicationRuntimeException;
import br.com.telescope.t2.feature.CustomFeature;
import br.com.telescope.t2.feature.MethodFeature;
import solutions.ferpa.ztest.ent.*;

/**
 * Testes com parâmetros de configuração.
 * <p>
 * null
 * <P>
 * Telescope origin: RTPREF.UTL_PREFERENCES
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class UtlPreferences extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "RTPREF.UTL_PREFERENCES";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Testes com parâmetros de configuração");
    }
    
    
    // Record items
    private DomUpper aKey;
    private DomText aValue;
    private DomText aDefaultValue;
    
    // Query items
    
    @Override
    public void initAttributes() {
        
        aKey = new DomUpper();
        aKey.context(ctx).name("KEY").label("Key").unbound(true);
        
        aValue = new DomText();
        aValue.context(ctx).name("VALUE").label("Value").unbound(true);
        
        aDefaultValue = new DomText();
        aDefaultValue.context(ctx).name("DEFAULT_VALUE").label("Default value").unbound(true);
        
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="CUSTOM">
    public class FCustom extends CustomFeature {
        
        @Override
        public void initForm() {
            addField(aKey, "opt");
            addField(aValue, "opt");
            addField(aDefaultValue, "opt");
        }
        
        @Override
        public void onAction() {
            addSubmit(getFeature("LER_CONFIG")).label("Ler configuração");
            addSubmit(getFeature("GET_CONFIG_DEFAULT")).label("Get config default");
            addSubmit(getFeature("SET_CONFIG")).label("Set config");
        }
        
        @Override
        public void execute() {
            try {
                custom();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("CUSTOM");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LER_CONFIG">
    public class FLerConfig extends MethodFeature {
        
        @Override
        public void run() {
            preRun();
            try {
                lerConfig();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("LER_CONFIG");
            setTitle("Ler configuração");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="GET_CONFIG_DEFAULT">
    public class FGetConfigDefault extends MethodFeature {
        
        @Override
        public void run() {
            preRun();
            try {
                getConfigDefault();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("GET_CONFIG_DEFAULT");
            setTitle("Get config default");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="SET_CONFIG">
    public class FSetConfig extends MethodFeature {
        
        @Override
        public void run() {
            preRun();
            try {
                setConfig();
            } catch (AdsException ex) {
                throw new ApplicationRuntimeException(ex);
            }
        }
        
        {
            setFeatureName("SET_CONFIG");
            setTitle("Set config");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FCustom());
        addFeature(new FLerConfig());
        addFeature(new FGetConfigDefault());
        addFeature(new FSetConfig());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "CUSTOM";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getKey() {
        return aKey.getValue();
    }
    
    public void setKey(String value) {
        this.aKey.setValue(value);
    }
    
    public String getValue() {
        return aValue.getValue();
    }
    
    public void setValue(String value) {
        this.aValue.setValue(value);
    }
    
    public String getDefaultValue() {
        return aDefaultValue.getValue();
    }
    
    public void setDefaultValue(String value) {
        this.aDefaultValue.setValue(value);
    }
    // </editor-fold>
    
    /**
     * null.
     */
    public void custom() throws AdsException {
        // <user-code id="858078" parentId="858076" hashcode="-42f29767" length="14">
        ctx.p("<hr>");
        // </user-code>
    }
    
    /**
     * Get config default.
     */
    private void getConfigDefault() throws AdsException {
        // <user-code id="858119" parentId="858115" hashcode="7d7a0f1c" length="392">
        String key = ctx.getParameter("A_KEY");
        String defaultValue = ctx.getParameter("A_DEFAULT_VALUE");
        ctx.p("<br>Config.getInstance().getParameter(\"" + key + "\", \"" + defaultValue + "\") = " + Config.getInstance().getParameter(key, defaultValue));
        ctx.p("<br>getPreference(\"" + key + "\", \"" + defaultValue + "\") = " + ctx.getPreference(key, defaultValue));
        getFeatures().get(0).run();
        // </user-code>
    }
    
    /**
     * Ler configuração.
     * <p>
     * Busca o valor de uma determinada chave de configuração sem informar 
     * valor default.
     * <P>
     * 
     * <P>
     */
    private void lerConfig() throws AdsException {
        // <user-code id="858094" parentId="858090" hashcode="-3368cff9" length="302">
        // import br.com.telescope.t2.config.Config;
        String key = ctx.getParameter("A_KEY");
        ctx.p("<br>Config.getInstance().getParameter(\"" + key + "\") = " + Config.getInstance().getParameter(key));
        ctx.p("<br>ctx.getPreference(\"" + key + "\") = " + ctx.getPreference(key));
        getFeatures().get(0).run();
        // </user-code>
    }
    
    /**
     * Set config.
     */
    private void setConfig() throws AdsException {
        // <user-code id="858125" parentId="858121" hashcode="52745d59" length="196">
        String key = ctx.getParameter("A_KEY");
        String value = ctx.getParameter("A_VALUE");
        ctx.p("setParameter(" + key + "," + value + ")");
        ctx.setPreference(key, value);
        getFeatures().get(0).run();
        // </user-code>
    }
}
